<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/11
 * Time: 11:55
 */
require_once APP_PATH."/app/ConfigManager.php";

class DB {

    private $db = null;

    private $table_name = '';

    public function __construct($connection = null){
        $connections = ConfigManager::getValue('connection','');
        if($connection){
            $connection = $connections[$connection];
        }else{
            $connection = $connections['pwbbs'];
        }
        $dsn = 'mysql:host='.$connection['host'].';dbname='.$connection['database'].';charset='.$connection['charset'];
        $this->db = new PDO($dsn, $connection['user'], $connection['password']);
    }

    /**
     * @param $table_name string 数据表名
     * @return object
     */
    public function table($table_name){
        $this->table_name = $table_name;
        return $this;
    }

    public function update(){

	}

    public function insert(){

    }

    public function select($fields = array(),$where = array(),$offset = '',$limit = ''){
        if($fields){
            if(is_array($fields)){
                $fields = implode(',',$fields);
            }
        }else{
            $fields = '*';
        }
        $sql = "SELECT $fields FROM pw_threads WHERE ";

        $stm = $this->db->prepare($sql);

        $stm->execute();

        return $stm->fetchAll();

    }


    public function find(){


    }

    public function delete($where = array()){

    }

    public function query($sql){
       $stm = $this->db->query($sql);
       return $stm->fetchAll();
    }

    public function execute($sql){
        return $this->db->exec($sql);
    }
}